var gsex = GetQueryString("gsex")
var id = GetQueryString("id")
// console.log(gsex)
if (gsex === '0') {
  $(".before-page").eq(1).html("男鞋").attr("href", "./goods-list.html?gsex=0")
} else if (gsex == 1) {
  $(".before-page").eq(1).html("女鞋").attr("href", "./goods-list.html?gsex=1")
} else {
  $(".before-page").eq(1).hide()
}

// 无id自动到首页
if(!id){
  location.href="./index.html"
}

$(function () {
  let isMobile = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
  if (isMobile) {
    $(document).delegate(".goods-details-container-left-select-area div", "click", function () {
      let largeSrc = $(this).find("img").data("largeSrc");
      $(".goods-details-max-img").attr("src", largeSrc);
      $(this).siblings().removeClass("goods-details-current")
      $(this).addClass("goods-details-current")
    })
  } else {
    $(document).delegate(".goods-details-container-left-select-area div", "mouseenter", function () {
      let largeSrc = $(this).find("img").data("largeSrc");
      $(".goods-details-max-img").attr("src", largeSrc);
      $(this).siblings().removeClass("goods-details-current")
      $(this).addClass("goods-details-current")
    })
  }

  $(document).delegate(".goods-details-info-details-lits-title", "click", function () {
    $(".goods-details-info-property").toggle()
  })

  $(document).delegate(".goods-details-info-desc-title", "click", function () {
    $(".goods-details-info-desc-txt").toggle()
  })


})

$.ajax({
  url: `${HOST}/index/good/goodDetails`,
  type: "post",
  data: {
    id
  },
  success: (res) => {
    // console.log(res)
    if (res.success == 1) {
      var data = res.data,
        str = "";
      data = manageRegTag(data)
      $(".selected-page").html(data.good_name)
      document.title = data.good_name + "--商品详情"
      $(".goods-details-info-goods-name").html(data.good_name)
      $(".goods-details-info-size-table").html()
      $(".goods-details-info-desc-txt").html(data.text)
      $(".goods-details-info-goods-unkwn").html(data.version)
      renderMainImg(data.imgs)
      renderThumb(data.cimgs)
      renderSize(data.size_ids)
      renderProperty(data.xiangqing)
    }
  }
})

function renderMainImg(e) {
  var btnArr = "",
    mainimg = `<div><img class="goods-details-max-img" src="${HOST+e[0]}" alt=""></div>`;
  e.forEach(element => {
    btnArr += `<div><img data-large-src="${HOST+element}" src="${HOST+element}"></div>`
  });
  $(".goods-details-container-left-large").html(mainimg)
  $(".goods-details-container-left-select-area").html(btnArr)
}

function renderThumb(e){
  var str = "";
  e.forEach(function(element){
    str += `<div><img src="${HOST+element}"></div>`
  })
  $(".goods-details-info-goods-type").html(str)
}

function renderSize(e){
  var str = "";
  e.forEach(function(element){
    str += `<span>${element}</span>`
  })
  $(".goods-details-info-size-table").html(str)
}

function renderProperty(e){
  var txtArr = ["制作","外部材料","内部材料","材质","闭合方式","鞋带","鞋底","高度","内部","缝隙"]
  var arr = [],str="";
  for(var item in e){
    arr.push({value:e[item]})
  }
  txtArr.forEach(function(item,i){
    arr[i].name=txtArr[i]
  })
  arr.forEach(function(item){
    str+=`<div>${item.name}：<span>${item.value}</span></div>`
  })

  $(".goods-details-info-property-list").html(str)

}